package com.weiwu.weiwustore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiwu.weiwustore.entity.Role;
import com.weiwu.weiwustore.mapper.MenusMapper;
import com.weiwu.weiwustore.mapper.RoleMapper;
import com.weiwu.weiwustore.pojo.RoleVo;
import com.weiwu.weiwustore.pojo.SelectItem;
import com.weiwu.weiwustore.service.RoleService;
import com.weiwu.weiwustore.utils.ObjectFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.*;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Autowired
    MenusMapper menusMapper;

    @Override
    public List<Role> getList() {
        return roleMapper.selectList(null);
    }

    @Override
    public List<SelectItem> findRoleItem() {
        List<SelectItem> res = new ArrayList<>();
        for (Role r : roleMapper.selectList(null)) {
            if (r.getId() == -1)
                continue;
            res.add(new SelectItem(r.getId().toString(), r.getRole()));
        }
        return res;
    }

    @Override
    public List<RoleVo> getroles() {
        return singlePackMenu(roleMapper.selectList(null));
    }

    @Override
    public Integer updaterolemenus(Map<String, Object> map) {
        List<Integer> movedKeys = (List<Integer>) map.get("movedKeys");
        Role role = roleMapper.selectById(ObjectFormatUtil.toInteger(map.get("id")));
        List<String> menus = Arrays.asList(role.getMenus().split(","));
        if ("right".equals(map.get("direction"))){
            StringBuffer menusadd = new StringBuffer();
            // 添加菜单权限
            menusadd.append(role.getMenus());
            for (int i = 0; i < movedKeys.size(); i++) {
                menusadd.append(","+movedKeys.get(i));
            }

            role.setMenus(menusadd.toString());
        }else {
            // 删除原有的菜单权限
            List<Integer> me = new ArrayList<>();
            StringBuilder delmenus = new StringBuilder();
            for (int i = 0; i < menus.size(); i++) {
                me.add(ObjectFormatUtil.toInteger(menus.get(i)));
            }
            me.removeAll(movedKeys);
            for (int i = 0; i < me.size(); i++) {
                delmenus.append(me.get(i)+",");
            }
            delmenus.deleteCharAt(delmenus.length() - 1);
            role.setMenus(delmenus.toString());
        }
        return roleMapper.updateById(role);
    }

    @Override
    public Integer addRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public Integer updateRole(Role role) {
        return roleMapper.updateById(role);
    }

    private List<RoleVo> singlePackMenu(List<Role> roles){
        List<RoleVo> res = new ArrayList<>();
        Map<Integer,String> menuss= new HashMap<>();
        for (int i = 0; i < roles.size(); i++) {
            menuss.put(i,splitStr(roles.get(i).getMenus()));
        }
        for (int i = 0; i < roles.size(); i++) {
            res.add(new RoleVo(roles.get(i).getId(),roles.get(i).getRole(),roles.get(i).getDescription(), menuss.get(i),
                    roles.get(i).getCreatetime(),roles.get(i).getUpdatetime()));
        }
        return res;
    }
    private String splitStr(String menus){
        StringBuffer stringBuffer = new StringBuffer();
        String[] menu = menus.split(",");
        for (int i = 0; i < menu.length; i++) {
            stringBuffer.append("["+menusMapper.selectById(menu[i]).getMenuname()+"]");
        }
        return stringBuffer.toString();
    }
}
